from django import forms
from .models import TouristAttraction, ScenicCategory, SentimentComment


class AttractionSearchForm(forms.Form):
    """景点搜索表单"""
    keyword = forms.CharField(
        max_length=100,
        required=False,
        label='关键词',
        widget=forms.TextInput(attrs={'placeholder': '搜索景点名称、地点...'})
    )
    city = forms.CharField(
        max_length=50,
        required=False,
        label='城市',
        widget=forms.TextInput(attrs={'placeholder': '输入城市名'})
    )
    category = forms.ModelChoiceField(
        queryset=ScenicCategory.objects.all(),
        required=False,
        label='分类',
        empty_label="全部分类"
    )
    min_heat = forms.IntegerField(
        required=False,
        label='最小热度',
        min_value=0,
        widget=forms.NumberInput(attrs={'placeholder': '0'})
    )


class AttractionFilterForm(forms.Form):
    """景点筛选表单"""
    SENTIMENT_CHOICES = [
        ('', '全部情感'),
        ('positive', '正面'),
        ('neutral', '中性'),
        ('negative', '负面'),
    ]

    SORT_CHOICES = [
        ('-heat_index', '热度排序'),
        ('-avg_sentiment_score', '情感得分排序'),
        ('-review_count', '评论数量排序'),
        ('name', '名称排序'),
    ]

    sentiment = forms.ChoiceField(
        choices=SENTIMENT_CHOICES,
        required=False,
        label='情感倾向'
    )
    sort_by = forms.ChoiceField(
        choices=SORT_CHOICES,
        required=False,
        label='排序方式',
        initial='-heat_index'
    )
    has_images = forms.BooleanField(
        required=False,
        label='仅显示有图片的景点'
    )


# main/forms.py
from django import forms
from .models import SentimentComment


class CommentForm(forms.ModelForm):
    """评论表单"""

    # 添加 sentiment_type 字段
    sentiment_type = forms.ChoiceField(
        choices=[
            ('positive', '正面'),
            ('neutral', '中性'),
            ('negative', '负面')
        ],
        widget=forms.Select(attrs={'class': 'form-control'}),
        label='情感倾向',
        initial='neutral'
    )

    class Meta:
        model = SentimentComment
        fields = ['content', 'rating', 'source_platform', 'sentiment_type']
        widgets = {
            'content': forms.Textarea(attrs={
                'rows': 3,
                'placeholder': '分享您对这个景点的看法...',
                'class': 'form-control'
            }),
            'rating': forms.RadioSelect(choices=[(i, str(i)) for i in range(1, 6)]),
            'source_platform': forms.HiddenInput(),  # 默认是网站用户
        }
        labels = {
            'content': '评论内容',
            'rating': '评分',
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # 设置 source_platform 的默认值
        self.fields['source_platform'].initial = 'website'


class ReplyForm(forms.ModelForm):
    """回复评论表单"""

    class Meta:
        model = SentimentComment
        fields = ['content']
        widgets = {
            'content': forms.Textarea(attrs={
                'rows': 2,
                'placeholder': '回复评论...',
                'class': 'form-control'
            }),
        }
        labels = {
            'content': '回复内容',
        }